package day20171010;

import java.io.*;
import java.sql.*;

/**
 * Created by 14394 on 2017/10/10.
 */
public class Test {
    Connection connection;
    PreparedStatement ps;
    public static void main(String[] args) throws IOException {
        new Test().queryBlob("1");

    }

    public void queryBlob(String id) throws IOException{
        connection=new InitDB().getConnection();
        String filename="D:\\dev\\test23.html";
        String sql="select * from spider1 where id=?";
        try {
            ps=connection.prepareStatement(sql);
            ps.setString(1,id);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                Blob b1=rs.getBlob("网页源码");
                InputStream is=b1.getBinaryStream();
                BufferedInputStream buffis=new BufferedInputStream(is);
                BufferedOutputStream buffout=new BufferedOutputStream(new FileOutputStream(filename));
                byte[] buf=new byte[1024];
                int len=buffis.read(buf,0,1024);
                while (len>0){
                    buffout.write(buf);
                    len=buffis.read(buf,0,1024);
                }
                buffout.flush();
                buffout.close();
                buffis.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
